unit uBackup;

interface
uses
  JvZlibMultiple, Classes, SysUtils, Forms;
type


  TBackup = class
  private
    FZip : TJvZlibMultiple;
    FArquivoBancodeDados: string;
    FOnProgress: TProgressEvent;
    FListaArquivos : TStringList;
    procedure SetArquivoBancodeDados(const Value: string);
    function GetBackupPath : String;
  public
    property ArquivoBancodeDados : string read FArquivoBancodeDados write SetArquivoBancodeDados;
    property OnProgress : TProgressEvent read FOnProgress write FOnProgress;
    function ExecutarBackup : Boolean;
    constructor Create;
    destructor Destroy;
  end;

implementation

{ TBackup }

constructor TBackup.Create;
begin
  FZip := TJvZlibMultiple.Create(nil);
  FListaArquivos := TStringList.Create;
end;

destructor TBackup.Destroy;
begin
  FreeAndNil(FZip);
  FreeAndNil(FListaArquivos);
end;

function TBackup.ExecutarBackup: Boolean;
begin
  try
    if Assigned(FOnProgress) then
      FZip.OnProgress := FOnProgress;
    FZip.CompressionLevel := 9;
    FZip.CompressFiles(FListaArquivos, GetBackupPath+'BACKUP_DIARIO_'+FormatDateTime('dd_mm_yyyy_hh_mm', now)+'.ocf');
    Result := true;
  except
    Result := false;
  end;
end;

function TBackup.GetBackupPath: String;
var
  ActualPath : string;
begin
  ActualPath := ExtractFilePath(Application.Exename);
  if not DirectoryExists(ActualPath+'\backups\') then
    MkDir(ActualPath+'\backups\');
  Result := ActualPath+'\backups\';
end;

procedure TBackup.SetArquivoBancodeDados(const Value: string);
begin
  FArquivoBancodeDados := Value;
  FListaArquivos.Clear;
  FListaArquivos.Add(Value);
end;

end.
